package com.zzl.springboot.exception;


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice   //注册Controller的控制对象，默认监控所有Controller
public class GlobalExceptionHandler {

    //处理异常
    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHandler(Exception ex, HttpServletRequest request) {

        //Controller发生异常时，将接收的异常对象ex传给页面exception.html
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("error", ex.getMessage());
        modelAndView.setViewName("error/exception");
        return modelAndView;
    }
}
